/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.vrangogym.beans.ctrl;

import edu.chl.vrangogym.beans.backing.EditUserDataBackingBean;
import edu.chl.vrangogym.persistence.entities.User;
import edu.chl.vrangogym.persistence.entities.values.Address;
import edu.chl.vrangogym.persistence.entities.values.ContactInfo;
import edu.chl.vrangogym.persistence.exception.NonexistentEntityException;
import edu.chl.vrangogym.persistence.impl.UserFacade;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * @author antonmalmquist
 */
@ManagedBean(name = "EditCtrl")
@RequestScoped
public class EditUserDataCtrlBean {

    @ManagedProperty(value = "#{Edit}")
    EditUserDataBackingBean editUserDataBackingBean;

    public void setEditUserDataBackingBean(EditUserDataBackingBean editUserDataBackingBean) {
        this.editUserDataBackingBean = editUserDataBackingBean;
    }
    @EJB
    UserFacade userFacade;

    /** Creates a new instance of EditUserDataCtrlBean */
    public EditUserDataCtrlBean() {
    }
    User sessionUser = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("Auth.user");

    public void edit(ActionEvent evt) {
        if (sessionUser != null
                && newPasswordEnteredAndValid()
                && oldPasswordCorrect()) {
            User user = updateUser();
            user.setPassword(editUserDataBackingBean.getNewPassword());
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("Auth.user", user);

            try {
                userFacade.edit(user);
                editUserDataBackingBean.updateUser();
            } catch (NonexistentEntityException nee) {
                System.err.println("User does not exist in database");
            }

        } else if (oldPasswordCorrect() && editUserDataBackingBean.getNewPassword().isEmpty()) {
            User user = updateUser();
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("Auth.user", user);
            try {
                userFacade.edit(user);
                editUserDataBackingBean.updateUser();
            } catch (NonexistentEntityException nee) {
                System.err.println("User does not exist in database");
            }

        } else {
            System.err.println("Password mismatch");
        }

    }

    private boolean newPasswordEnteredAndValid() {
        return (editUserDataBackingBean.getNewPassword().equals(editUserDataBackingBean.getNewPassword2())
                && !(editUserDataBackingBean.getNewPassword().isEmpty()));
    }

    private boolean oldPasswordCorrect() {
        return (editUserDataBackingBean.getOldPassword().equals(sessionUser.getPassword()));
    }

    private User updateUser() {
        User user = userFacade.find(sessionUser.getUserName());
        ContactInfo contactInfo;
        Address address;

        address = new Address(editUserDataBackingBean.getStreet(),
                editUserDataBackingBean.getZipCode(),
                editUserDataBackingBean.getCity());

        contactInfo = new ContactInfo(editUserDataBackingBean.getFirstname(),
                editUserDataBackingBean.getLastname(),
                address,
                editUserDataBackingBean.getMail(),
                editUserDataBackingBean.getPhone());
        user.setContactInfo(contactInfo);
        return user;
    }
}
